package com.copa.l566;

/**
 * @author copa
 * @createDate 2022-08-31 12:03
 * @function 一次遍历法
 */
public class Test1 {

    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if (m * n != r * c || m == r || n == c) {
            return mat;
        }
        int[][] result = new int[r][c];
        int tempR = 0;
        int tempC = 0;
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                result[tempR][tempC] = mat[i][j];
                tempC++;
                if (tempC == c) {
                    tempR++;
                    tempC = 0;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] mat = {
                {1,2},
                {3,4}
        };
        int r = 2;
        int c = 4;
        int[][] result = new Test1().matrixReshape(mat, r, c);
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                System.out.print(result[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
